import torch
from torch.nn import L1Loss, MSELoss, CrossEntropyLoss

inputs = torch.tensor([1, 2, 3], dtype=torch.float)
targets = torch.tensor([1, 2, 5], dtype=torch.float)

inputs = torch.reshape(inputs, (1, 1, 1, 3))
targets = torch.reshape(targets, (1, 1, 1, 3))

# 均分差
# (1,2,3)-(1,2,5) = (0,0,2)/3=0.66666667
loss = L1Loss()
result = loss(inputs, targets)

# 平方均分差
# (1,2,3)-(1,2,5) = (0,0,2^2)/3=1.33333
loss_mse = MSELoss()
result_mse = loss_mse(inputs, targets)

print(result)
print(result_mse)

# 交叉熵计算
# -x[class]+sum_j(ln(e^j)) => (-0.2)+ln(exp(0.1)+exp(0.2)+exp(0.3))
x = torch.tensor([0.1, 0.2, 0.3])
# 这里为1是因为target [people,dog,cat] = [0,1,2] 类似于键值对映射关系
y = torch.tensor([1])
# 1是输入1个图片  3是指分别由people，dog，cat 3个分类target
x = torch.reshape(x, (1, 3))
loss_cross = CrossEntropyLoss()
result_cross = loss_cross(x, y)
print(result_cross)
